#include "bowenview.h"
#include <QGradient>
#include <ctime>
#include <QPointF>
#include <QVector>
#include <QPainterPath>
#include <QPolygonF>
#include <QLinearGradient>
#include "../configs/configfile.h"

BowenView::BowenView(QQuickItem *parent)
    :QQuickPaintedItem(parent)
{
    setFlag(ItemAcceptsInputMethod,true);

    m_currentData.clear();

    //        for(int i = 0;i < 600 ;i ++){
    //            if(i==0){
    //                m_currentData.append(15);
    //            }else{
    //                if(i==599){
    //                    m_currentData.append(5);
    //                }else{
    //                    m_currentData.append((i%20)*1.0 / 3);
    //                }

    //            }

    //        }

    m_allNum = ConfigFile::gestance()->getSinW();//180 * 2;
    m_allHeight =ConfigFile::gestance()->getSinH();// 10;

    update();

}

BowenView::~BowenView()
{

}

int BowenView::getMaxValue()
{
    return m_allHeight;

}

void BowenView::clearValue()
{
    m_currentData.clear();

    update();
}

void BowenView::insertValue(double valuse)
{
    //    static double numbers=1.0;

    //    if(numbers >= 2){
    //        numbers=1.0;
    //    }
    //    m_currentData.append(valuse  / 1000 +numbers);
    //    numbers += 0.1;

    m_currentData.append(valuse * 1.0  );
    update();
}

void BowenView::paint(QPainter *painter)
{
    int allNum = m_allNum;

    painter->setRenderHint(QPainter::Antialiasing);

    int counts = m_currentData.count();
    // painter->setPen(QPen(QColor("#999999"),3));
    int withLens = this->width() - 1;
    int lenSpace = 30;

    //  painter->drawLine(QPointF(15,0),QPointF(15,this->height() ));


    for(int i =1 ;i <= m_allHeight; i++){
        if(i != m_allHeight){

            painter->setPen(QPen(QColor("#303030"),1));
            painter->drawText(QPointF(0,this->height() - i * ( this->height() / m_allHeight)),QString::number(i));
            painter->setPen(QPen(QColor("#24B9B7"),0.5));

        }else{
            painter->setPen(QPen(QColor("#24B9B7"),1));

        }

        painter->drawLine(QPointF(lenSpace,this->height() - i * ( this->height() / m_allHeight)),QPointF(this->width() ,this->height() - i * ( this->height() / m_allHeight) ));

    }
    double withs = 1.0 * this->width() /( allNum - 1);

    if(counts > 0){
        QVector<QPointF> lists;
        QVector<QPointF> lista;
        int temps = 0;
        int tempall = counts;
        if(counts > m_allNum){
            temps = counts - m_allNum;
            tempall = m_allNum;
        }
        for(int i = temps ;i < counts; i++){
            QPointF pos;
            pos.setX(withs  * (i - temps)+lenSpace);
            pos.setY((m_allHeight - m_currentData[i]) *this->height() / m_allHeight );
            lists.append(pos);
        }

        painter->setPen(QPen(QColor(78,137,248),1));
        painter->drawPolyline(QPolygonF(lists));
        painter->setPen(QPen(QColor(78,137,248,0),1));
        lista.append(lists[tempall - 1]);

        lista.append(QPointF(lists[tempall - 1].x(),this->height() ));
        lista.append(QPointF(lists[0].x(),this->height() ));
        lista.append(QPointF(lists[0].x(),lists[0].y() ));
        // painter.drawPolyline(QPolygonF(lista));
        lista.append(lists);
        QPainterPath paths;
        QLinearGradient myGradient(this->width() / 2,0 ,this->width() / 2,this->height());
        myGradient.setColorAt(0.2,QColor(78,137,248,255 *0.5));
        // myGradient.setColorAt(0.7,QColor(Qt::red));
        myGradient.setColorAt(1,QColor(255,255,255,255 *0.5));
        paths.addPolygon(QPolygonF(lista));
        //painter.setBrush(Qt::SolidPattern);
        //  painter.setBrush(QBrush(QColor(0,212,115),Qt::SolidPattern));//   QBrush bura(,Qt::SolidPattern);
        painter->setBrush(myGradient);
        painter->drawPath(paths);

    }
    painter->setPen(QPen(QColor("#303030"),1));
    painter->drawText(QPointF(0,this->height()),QString::number(0));

    painter->setPen(QPen(QColor("#24B9B7"),1));
    painter->drawLine(QPointF(lenSpace,this->height()),QPointF(withLens,this->height() ));

    //    for(int i =0 ;i < m_allNum; i++){
    //      painter->drawLine(QPointF(15+withs * i,this->height()),QPointF(15+withs * i,this->height() -10 ));

    //    }
}
